<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
    </resultMap>

</#if>
<#if baseColumnList>

    <sql id="base_columns">
<#list table.fields as field>
        t1.${field.columnName},
</#list>
    </sql>

</#if>
    <#if buildType == 'BILL' || buildType == 'NORMAL'>

    <sql id="base_sql">
        select
            t1.*,
            creator.staff_name as "creatorName",
            updater.staff_name as "updaterName"
        from ${table.name} t1
        left join sys_user creator on creator.id = t1.creator_id
        left join sys_user updater on updater.id = t1.updater_id
        where t1.del_flag = '0'
    </sql>

    <sql id="base_queryObj_sql">
        <#list table.fields as field>
            <#if field.propertyName == "creatorId">
            <#elseif field.propertyName == "creatorName" >
            <#elseif field.propertyName == "createTime" >
            <#elseif field.propertyName == "updaterId" >
            <#elseif field.propertyName == "updaterName" >
            <#elseif field.propertyName == "updateTime" >
            <#elseif field.propertyName == "delFlag" >
            <#else>
            <#if field.customMap??>
                <#list field.customMap?keys as key>
                <#if key == "qc" >
                    <#if field.customMap[key] == "eq" >
        <#noparse><if test=</#noparse>"queryObj.${field.propertyName} != null and queryObj.${field.propertyName} != '' "<#noparse>></#noparse>
            and t1.${field.columnName} = <#noparse>#{queryObj.</#noparse>${field.propertyName}<#noparse>}</#noparse>
        <#noparse></if></#noparse>
                    <#elseif field.customMap[key] == "like" >
        <#noparse><if test=</#noparse>"queryObj.${field.propertyName} != null and queryObj.${field.propertyName} != '' "<#noparse>></#noparse>
            and t1.${field.columnName} like concat('%',<#noparse>#{queryObj.</#noparse>${field.propertyName}<#noparse>}</#noparse>,'%')
        <#noparse></if></#noparse>
                    <#else>
                    </#if>
                </#if>
                </#list>
            </#if>
            </#if>
        </#list>
    </sql>


    <sql id="base_sort_sql">
        <choose>
            <when test="queryObj.orderBy != null and queryObj.orderBy != '' ">
                ORDER BY <#noparse>${queryObj.orderBy}</#noparse>
                <if test="queryObj.sortOrder != null and queryObj.sortOrder != '' ">
                    <#noparse>${queryObj.sortOrder}</#noparse>
                </if>
            </when>
            <otherwise>
                ORDER BY id desc
            </otherwise>
        </choose>
    </sql>


    <select id="pageVo" resultType="${package.RespVoPackage}.${RespVo}">
        <#noparse><include refid="base_sql"/></#noparse>
        <#noparse><include refid="base_queryObj_sql"/></#noparse>
        <#noparse><include refid="base_sort_sql"/></#noparse>
    </select>


    <select id="listVo" resultType="${package.RespVoPackage}.${RespVo}">
        <#noparse><include refid="base_sql"/></#noparse>
        <#noparse><include refid="base_queryObj_sql"/></#noparse>
        <#noparse><include refid="base_sort_sql"/></#noparse>
    </select>


    <select id="getVo" resultType="${package.RespVoPackage}.${RespVo}">
        <#noparse><include refid="base_sql"/></#noparse>
          and t1.id = <#noparse>#{id}</#noparse>
    </select>

    </#if>

    <#if buildType == 'ITEM' >

    <select id="listVoBy${firstLetterUpperForeignProperty}" resultType="${package.RespVoPackage}.${billClass}RespVo$Inner${billClass}RespVoItem">
        select
            t1.*
         from ${table.name} t1
        where t1.del_flag = '0'
          and t1.${foreignKey} = <#noparse>#{</#noparse>${foreignProperty}<#noparse>}</#noparse>
    </select>

    </#if>



</mapper>
